\newpage
\section{Taylor series}

\subsection{Introduction}
One of the fundamental ideas in differential calculus is that a function can be locally approximated (i.e at a point $x_0$) by its tangent line. For example, consider the function $f(x) = \sin{x}$ at $x_0=\pi$. 

The tangent line at $x_0$ is

\begin{equation}
y(x) = f(x_0) + f'(x_0)(x-x_0) 
\end{equation}

Subtitution in our equation gives:

\begin{equation*}
y(x) = sin(x_0) + cos(x_0)(x-x_0) 
\end{equation*}

\begin{sagesilent}
f(x) = sin(pi) + cos(pi)*(x-pi) 
ptangent = plot(f, 0, 2*pi, rgbcolor='red')
psin = plot(sin(x), 0, 2*pi, rgbcolor='black')
\end{sagesilent}

\sageplot[width=8cm]{
plot(ptangent+psin, fontsize=16, ymax=1, ymin=-1)
}

The Taylor series is the expansion series of a function $f(x)$ about a point $x_0$. If the Taylor series takes $x_0=0$ that series is called Maclaurin series.

\subsection{Definition}
The Taylor series transforms a function $f(x)$ into a polynomial expression with an infinite number of terms around a point $x_0$. Each term in the expression uses information about higher derivatives to replace the original function by a polynomial.

\begin{equation*}
f(x) = f(x_0) + \frac{f'(x+0)}{1!}(x-x_0) + \cdots + \frac{f^{(n)}(x_0)}{n!}(x-x_0)^n
\end{equation*}

This can be written in sigma notation form as:
\begin{equation*}
f(x) = \sum_{n=0}^{\infty}\frac{f^{(n)}(x_0)}{n!} \, (x-x_0)^{n}
\end{equation*}

Or sometimes by the alternative expression:
\begin{equation*}
f(x_0 + \Delta x) = \sum_{n=0}^{\infty}\frac{f^{(n)}(x_0)}{n!} \, \Delta x^{n}
\end{equation*}

This expression expands the variable $x_0$ around $\Delta x$ for the function $f(x)$ with a degree $n$. As we increase the degree of the Taylor polynomial of the function, the approximation of the function by its Taylor expression becomes more and more accurate.

\subsection{Taylor expansion in Sage}
In Sage we can get the analytical or numerical expression of a Taylor series. For example, to get the first five terms of the function $f(x) = x^3 + x^2 + x + 1$ around $x_0$ we do:
\begin{sageblock}
x_0 = var('x_0')
f(x) = x^3 + x^2 + x +1
# get the 5 degree expansion of f(x)
ftaylor(x) = taylor(f, x, 'x_0', 5)
\end{sageblock}

We can now evaluate this expression numerically around $x_0=0$ and for  $x=10$
\begin{sageblock}

fsol(x) = ftaylor.subs(x_0 = 0)
fsol(10)
\end{sageblock}

which returns the value \sage{fsol(10)}. Note that the result of the Taylor expression at this point is the same as the value of the function at this point, therefore, the 5th degree expansion is sufficient to describe the function until the value $x=10$.

\subsection{Geometrical interpretation}
The Taylor series approximate a function by using a finite number of terms. Generally, the higher this number, the better is the approximation for values which are far away from the point that we are using to use the expansion. 

As example, we can plot the different the series expansion for the function $f(x)=\sin{x}$ at $x_0=\pi$ for different degree of the Taylor series.

To get the polynomial expression of degree 1 we need 2 terms of the Taylor expansion:

\begin{equation*}
f(x) \approx \sin{\pi} + \cos{\pi}(x-\pi) \approx (x-\pi)
\end{equation*}

which is the equation for a line. For the polynomial of degree 3:

\begin{equation*}
f(x) \approx \sin{\pi} + \cos{\pi}(x-\pi) -\sin{\pi}\frac{(x-\pi)^2}{2} - \cos{\pi}\frac{(x-\pi)^3}{6}
\end{equation*}

%And degree 5:
\begin{equation*}
f(x) \approx \sin{\pi} + \cos{\pi}(x-\pi) -
             \sin{\pi}\frac{(x-\pi)^2}{2} - 
             \cos{\pi}\frac{(x-\pi)^3}{6} +   
             \sin{\pi}\frac{(x-\pi)^4}{24} + 
             \cos{\pi}\frac{(x-\pi)^5}{120}
%
\end{equation*}
\begin{sageblock}
f(x) = sin(x)
p1 = f.taylor(x, pi, 1) # polynomial degree 1
p3 = f.taylor(x, pi, 3) # polynomial degree 3
p5 = f.taylor(x, pi, 5) # polynomial degree 5

\end{sageblock}

\begin{sagesilent}
psin = plot(f, 0, 2*pi, rgbcolor='black')
p1plot = plot(p1, 0, 2*pi, rgbcolor='orange')
p3plot = plot(p3, 0, 2*pi, rgbcolor='green')
p5plot = plot(p5, 0, 2*pi, rgbcolor='blue')
\end{sagesilent}


\sageplot[width=8cm]{
plot(psin+p1plot+p3plot+p5plot, fontsize=16, ymax=1, ymin=-1)
}

\subsection{Some common Taylor series}

The Taylor series for the exponential function $f(x) = e^x$ at $x_0=0$
\begin{equation*}
e^x = 1+\frac{x^1}{1!} + \frac{x^2}{2!} + \frac{x^3}{3!} + \frac{x^4}{4!} + \frac{x^5}{5!}+ \cdots = \sum_{n=0}^{\infty} \frac{x^n}{n!}.
\end{equation*}
